/**
 * Navigator.jsx
 * 导航页
 *
 * created by qiuwei
 * version 1.0.0
 * 2016年03月15日
 */

import React from 'react';
import { connect } from 'react-redux';
import { push } from 'react-router-redux';

import { Menu, MenuItem } from '../components/smart';

import * as NavigatorStyle from './Navigator.less';
import cx from 'classnames';
import { CITY, COUNTY, SCHOOL, NONE } from '../../../constants/area';
import { switchSubView } from '../actions/level3Navigator';

import { selectedCity,
	     selectedCounty,
	     selectedSchool,
	     /*获取年级数据*/
	 	 getGradeOptions,
	 	 getSubjectOptions,
	 	 /*获取地域数据*/
	 	 getCityOptions,
	 	 getCountyOptions,
	 	 getCountyOptionsSuccess,
	 	 getSchoolOptions,
	 	 getSchoolOptionsSuccess,
	 	 reset as filterReset
	 	 	} from '../actions/filter';	 

import iconLayout from '../../resources/menu_item_layout.png';
import iconQuality from '../../resources/menu_item_quality.png';
import iconBuild from '../../resources/menu_item_build.png';
import iconSetting from '../../resources/menu_item_setting.png';

class Navigator extends React.Component {

	constructor(props) {
		super(props);
	}
	//加载时鉴定权限
	componentWillMount() {
		let { authentication, filter, level3Navigator, dispatch } = this.props;

		let right = parseInt(authentication.user.right);
		let items = authentication.user.item_ids;

		let options = items.map((n) => 
			({ 
				enum: n.item_id, 
				name: n.name, 
				parentId: n.parent_id, 
				parentName: n.parent_name 
			}));

		let defaultFirstOption = null;
		if (options && options.length > 0)
			defaultFirstOption = options[0];

		let defaultSelectedAction, mountSubOptionsAction, switchAction;
		// 校级
		if (right == 1) {
			switchAction = switchSubView(SCHOOL);
			dispatch(getSchoolOptionsSuccess(options));

			defaultSelectedAction = selectedSchool;
		}
		// 区县级
		else if (right == 2) {
			switchAction = switchSubView(COUNTY);
			dispatch(getCountyOptionsSuccess(options));

			defaultSelectedAction = selectedCounty;
			mountSubOptionsAction = getSchoolOptions;
		}
		// 市级
		else if (right == 3) {
			switchAction = switchSubView(CITY);
			dispatch(getCityOptions(options));

			defaultSelectedAction = selectedCity;
			mountSubOptionsAction = getCountyOptions;
		}

		if (switchAction && level3Navigator.subView == NONE) 
			dispatch(switchAction);

		// 默认选择第一个
		if (defaultFirstOption && defaultSelectedAction) {
			dispatch(defaultSelectedAction(defaultFirstOption));

			// 挂载下一级数据
			if (mountSubOptionsAction) {
				dispatch(mountSubOptionsAction(defaultFirstOption.enum));
			}
		}
		dispatch(getSubjectOptions());
		dispatch(getGradeOptions());
		
	};
	/**
	 * 点击菜单项跳转到预订路径
	 * @param  {String} path 目标路径
	 */
	handleMenuItemClick = (path) => {
		let { dispatch } = this.props;
		dispatch(push(path));
		/*点击可选菜单跳转页面顶部*/
		scrollTo(0,0);
	};
	
	render() {
		let { children, routing } = this.props;

		let { locationBeforeTransitions } = routing;
		let { pathname } = locationBeforeTransitions;

		return (
			<div className={NavigatorStyle.navigator}>
				<div className='leftMenu'>
					<Menu>
						<MenuItem primaryText='教育布局' style={{cursor: 'default'}} icon={iconLayout} >
							<MenuItem primaryText='数字化教学概览' isSelected={/\/app\/navigator(\/basicOverview)?$/i.test(pathname)}
								path='/app/navigator/basicOverview' 
								onClick={this.handleMenuItemClick} />
							<MenuItem primaryText='电子教学日志' isSelected={'/app/navigator/TeachingJournal' == pathname}
								path='/app/navigator/TeachingJournal'
								onClick={this.handleMenuItemClick}
							/>
							<MenuItem primaryText='教育排行榜' 
							path='/app/navigator/tempPaper'
							onClick={this.handleMenuItemClick}
							/>
						</MenuItem>
						<MenuItem primaryText='教学质量' style={{cursor: 'default'}} icon={iconQuality} >
							<MenuItem primaryText='作业管理'  isSelected={'/app/navigator/homeWorkManagement' == pathname}
							path='/app/navigator/homeWorkManagement'
							onClick={this.handleMenuItemClick}/>
							<MenuItem primaryText='质量反馈'   isSelected={'/app/navigator/qualityFeedback' == pathname}
							path='/app/navigator/qualityFeedback'
							onClick={this.handleMenuItemClick}/>
						</MenuItem>
						<MenuItem primaryText='能力建设' style={{cursor: 'default'}} icon={iconBuild} >
							<MenuItem primaryText='学生能力建设' 
							path='/app/navigator/tempPaper'
							onClick={this.handleMenuItemClick}/>
							<MenuItem primaryText='教师能力建设'  
							path='/app/navigator/tempPaper'
							onClick={this.handleMenuItemClick}/>
						</MenuItem>
						<MenuItem primaryText='系统设置' style={{cursor: 'default'}} icon={iconSetting} >
							<MenuItem primaryText='更改密码'  isSelected={'/app/navigator/ChangeThePassword' == pathname}
							path='/app/navigator/ChangeThePassword'
							onClick={this.handleMenuItemClick}/>
							<MenuItem primaryText='意见反馈' 
							path='/app/navigator/tempPaper'
							onClick={this.handleMenuItemClick}/>
							<MenuItem primaryText='关于大象' 
							path='/app/navigator/tempPaper'
							onClick={this.handleMenuItemClick}/>
							<MenuItem primaryText='联系我们' 
							path='/app/navigator/tempPaper'
							onClick={this.handleMenuItemClick}/>
						</MenuItem>
					</Menu>
				</div>
				<div style={{padding: '0 0 0 10px'}}>
					{ children }
				</div>
			</div>
		);
	}
}

export default connect((state) => state)(Navigator);